package cn.edu.imust.loginregisterdemo.forgetpwd;

import android.os.Bundle;
import android.view.View;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;

import cn.bmob.v3.Bmob;
import cn.bmob.v3.BmobSMS;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.QueryListener;
import cn.bmob.v3.listener.UpdateListener;
import cn.edu.imust.loginregisterdemo.R;
import cn.edu.imust.loginregisterdemo.TimeCount;
import cn.edu.imust.loginregisterdemo.databinding.ActivityForgetPasswordBinding;
import cn.edu.imust.loginregisterdemo.utils.AccountValidatorUtil;
import cn.edu.imust.loginregisterdemo.utils.Utils;

public class ForgetPasswordActivity extends AppCompatActivity {

    private ActivityForgetPasswordBinding binding;
    private String currentPhoneNum;
    private String code;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_forget_password);
        Bmob.initialize(this, "33452dfaa0b4f1b9b1a5ea2c1818d5f4");
        currentPhoneNum = binding.phone.getText().toString().trim();
        code = binding.smsContent.getText().toString().trim();
        String pwd1 = binding.pwd1.getText().toString().trim();
        String pwd2 = binding.pwd2.getText().toString().trim();
        binding.getSms.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (AccountValidatorUtil.isMobile(currentPhoneNum) || pwd1.equals(pwd2) || AccountValidatorUtil.isPassword(pwd1) || AccountValidatorUtil.isPassword(pwd2)) {
                    new TimeCount(binding.getSms, 60 * 1000, 1000)
                            .start();
                    //TODO  手机验证码登录
                    /**
                     * TODO template 如果是自定义短信模板，此处替换为你在控制台设置的自定义短信模板名称；如果没有对应的自定义短信模板，则使用默认短信模板。
                     */
                    BmobSMS.requestSMSCode(currentPhoneNum, "牧场记录", new QueryListener<Integer>() {
                        @Override
                        public void done(Integer smsId, BmobException e) {
                            if (e == null) {
                                Utils.showSnackBar(ForgetPasswordActivity.this, v, "发送验证码成功，短信ID：" + smsId);
                                binding.confirm.setOnClickListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        BmobUser.resetPasswordBySMSCode(code, pwd2, new UpdateListener() {
                                            @Override
                                            public void done(BmobException e) {
                                                if (e == null) {
                                                    Utils.showSnackBar(ForgetPasswordActivity.this, v, "重置成功");
                                                } else {
                                                    Utils.showSnackBar(ForgetPasswordActivity.this, v, "重置失败");
                                                }
                                            }
                                        });
                                    }
                                });
                            } else {
                                Utils.showSnackBar(ForgetPasswordActivity.this, v, "发送验证码失败");
                            }
                        }
                    });
                }

            }
        });
    }


}
